﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using Diploma.Models;
using DiplomaWeb.Models.CompaniesAndLecturers;
using DiplomaWeb.Models.ConsultantOfPart;
using DiplomaWeb.Models.Reviewer;
using DiplomaWeb.Models.Student;

namespace DiplomaWeb.Controllers
{
    [Authorize(Roles = "SystemManager")]
    public class SystemManagersController : MasterController
    {
        public SystemManagersController(DataManager dataManager) : base(dataManager)
        {
        }

        public ActionResult Home()
        {
            ViewData["Levels"] = DataManager.QualificationLevelsRepository.GetAllLevels();
            return View();
        }

        public ActionResult Planings()
        {
            ViewData["LocalLanguage"] = ((CultureInfo) Session["Culture"]).Name;
            ViewData.Model = DataManager.QualificationLevelsRepository.GetAllLevels();
            return View("~/Views/SystemManagers/Planings/Planings.aspx");
        }

        public ActionResult News()
        {
            return View();
        }

        public ActionResult UsersManagement()
        {
            return View();
        }

        public ActionResult PlaningsForLevel(int id)
        {
            QualificationLevel level = DataManager.QualificationLevelsRepository.GetLevel(id);
            if (level == null)
                throw new Exception();
            ViewData.Model = DataManager.PlaningsRepository.GetAllPlaningTypes();
            ViewData["LevelId"] = level.QualificationLevelId;

            return PartialView("~/Views/SystemManagers/Planings/PlaningControlsList.ascx");
        }

        public ActionResult GetAllStudents()
        {
            ViewData["ActiveUsersPartialHtml"] = RenderPartialViewToString("~/Views/Students/AllStudentsControl.ascx",
                                                                           DataManager.QualificationLevelsRepository.
                                                                               GetAllLevels());
            ViewData["GetDeletedEntriesLink"] = Url.RouteUrl("Default",
                                                             new {controller = "Students", action = "GetDeleted"});


            return PartialView("~/Views/SystemManagers/UsersListControl.ascx");
        }

        public ActionResult GetAllStudentsGroups()
        {
            ViewData["ActiveUsersPartialHtml"] =
                RenderPartialViewToString("~/Views/StudentsGroups/AllGroupsControl.ascx",
                                          DataManager.QualificationLevelsRepository.GetAllLevels());
            ViewData["GetDeletedEntriesLink"] = Url.RouteUrl("Default",
                                                             new {controller = "StudentsGroups", action = "GetDeleted"});

            return PartialView("~/Views/SystemManagers/UsersListControl.ascx");
        }

        public ActionResult GetAllLecturers()
        {
            ViewData["ActiveUsersPartialHtml"] = RenderPartialViewToString(
                "~/Views/Lecturers/AllLecturersControl.ascx", DataManager.LecturersRepository.GetAllLecturers());
            ViewData["GetDeletedEntriesLink"] = Url.RouteUrl("Default",
                                                             new {controller = "Lecturers", action = "GetDeleted"});

            return PartialView("~/Views/SystemManagers/UsersListControl.ascx");
        }


        #region CompaniesAndLecturers

        public ActionResult CompaniesAndLecturers()
        {
            return View("~/Views/SystemManagers/CompaniesAndLecturers/CompaniesAndLecturers.aspx");
        }

        public ActionResult CompaniesList()
        {
            var viewModel = new CompanyListViewModel();
            viewModel.ToViewModel(DataManager.CompaniesRepository.GetAll());
            return View("~/Views/SystemManagers/CompaniesAndLecturers/CompaniesList.cshtml", viewModel);
        }

        public ActionResult LecturersList()
        {
            var viewModel = new LecturerPracticeListViewModel();
            viewModel.ToViewModel(DataManager.LecturersRepository.GetLecturersForPractice());
            return View("~/Views/SystemManagers/CompaniesAndLecturers/LecturersList.cshtml", viewModel);
        }


        public ActionResult StudentGroupsList()
        {
            var viewModel = new StudentGroupsViewModel();
            viewModel.ToViewModel(DataManager.StudentsGroupsRepository.GetAllGroups());
            return View("~/Views/SystemManagers/CompaniesAndLecturers/StudentGroupsList.cshtml", viewModel);
        }

        public ActionResult GetCompanyDetails(Guid companyId)
        {
            var viewModel = new CompanyPropertiesViewModel();
            viewModel.ToViewMode(DataManager.CompaniesRepository.Get(companyId));
            return View("~/Views/SystemManagers/CompaniesAndLecturers/CompanyProperties.cshtml", viewModel);
        }

        public ActionResult GetLecturerPracticeDetails(Guid lecturerId)
        {
            var viewModel = new LecturerPracticeViewModel();
            var lecturer = DataManager.LecturersRepository.GetLecturer(lecturerId);
            var lecturerForPractice = DataManager.LecturersRepository.GetLevelsInfo(lecturerId);
            var companiesForLevel = DataManager.CompaniesRepository.LecturerInCompanies(lecturerId);
            var students = DataManager.StudentsRepository.GetStudentsForLecturerPractice(lecturerId);
            viewModel.ToViewModel(lecturer, lecturerForPractice, companiesForLevel, students);
            return View("~/Views/SystemManagers/CompaniesAndLecturers/LEcturerProperties.cshtml", viewModel);
        }

        public ActionResult GetGroupPracticeDetails(Guid groupId)
        {
            var viewModel = new StudentsPracticeViewModel();
            viewModel.ToViewModel(DataManager.StudentsRepository.GetStudents(groupId));
            return View("~/Views/SystemManagers/CompaniesAndLecturers/GroupPracticeProperties.cshtml", viewModel);
        }

        public ActionResult UpdateCompanyProperties(CompanyPropertiesViewModel company)
        {
            DataManager.CompaniesRepository.UpdateCompanyName(company.Id, company.Name);

            foreach (CompanyLevelsViewModel item in company.Levels)
            {
                DataManager.CompaniesRepository.DetachCompanyFromLevel(company.Id, item.Id);
                if(item.IsCheck)
                {
                    DataManager.CompaniesRepository.AttachCompanyToLevel(company.Id, item.Id, item.MaxStudents);
                }
            }

            return RedirectToRoute("Default", new { controller = "SystemManagers", action = "CompaniesAndLecturers" });
        }

        public ActionResult UpdateStudentPractice(StudentsPracticeViewModel model)
        {
            foreach (StudentViewModel s in model.Students)
            {
                DataManager.StudentsRepository.UpdatePracticePlace(s.Id, s.PracticePlaceId);
            }

            return RedirectToRoute("Default", new { controller = "SystemManagers", action = "CompaniesAndLecturers" });
        }

        #region Ajax

        public JsonResult AddCompany(string companyName)
        {
            DataManager.CompaniesRepository.Add(companyName);
            return Json(new { });
        }

        public JsonResult AddLecturer(Guid lecturerId)
        {
            DataManager.LecturersRepository.AddForPractice(lecturerId);
            return Json(new { });
        }

        public JsonResult AddCompanyForLecturer(Guid lecturerId, int levelId, Guid companyId)
        {
            DataManager.CompaniesRepository.AttachCompanyToLecturer(lecturerId, companyId, levelId);
            return Json(new { });
        }

        public JsonResult AddStudentForLecturer(Guid studentId, Guid lecturerId)
        {
            DataManager.StudentsRepository.AttachLecturerPracticeForStudent(studentId, lecturerId);
            return Json(new { });
        }

        public JsonResult DeleteCompanyFromLecturer(Guid lecturerId, int levelId, Guid companyId)
        {
            DataManager.CompaniesRepository.DetachCompanyToLecturer(lecturerId, companyId, levelId);
            return Json(new { });
        }

        public JsonResult DeleteStudentFromLecturer(Guid studentId)
        {
            DataManager.StudentsRepository.DetachLecturerPracticeFromStudent(studentId);
            return Json(new { });
        }

        public JsonResult DeleteCompany(Guid companyId)
        {
            DataManager.CompaniesRepository.Delete(companyId);
            return Json(new { });
        }

        public JsonResult DeleteLecturerPractice(Guid lecturerId)
        {
            DataManager.LecturersRepository.RemoveForPractice(lecturerId);
            return Json(new { });
        }

        public JsonResult UpdateLecturerLoading(Guid lecturerId, int levelId, int value)
        {
            DataManager.LecturersRepository.UpdateLecturerLoadingForPractice(lecturerId, levelId, value);
            return Json(new { });
        }

        #endregion

        #endregion

        #region Work Places

        public ActionResult UsersPlaces()
        {
            var viewModel = new StudentProgressViewModel();
            viewModel.ToViewModel(DataManager.StudentsGroupsRepository.GetAllGroups());
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult UsersPlaces(StudentsProgressViewModel model)
        {
            foreach (var item in model.Students)
            {
                DataManager.StudentsRepository.UpdateUserWorkPlace(item.Id, item.Placement);
            }

            var viewModel = new StudentProgressViewModel();
            viewModel.ToViewModel(DataManager.StudentsGroupsRepository.GetAllGroups());
            viewModel.IsSaved = true;
            return View(viewModel);
        }

        public PartialViewResult StudentsPart(Guid groupId)
        {
            var viewModel = new StudentsProgressViewModel();
            var students = DataManager.StudentsRepository.GetStudents(groupId);
            foreach (var s in students)
            {
                var item = new StudentProgressViewModel();
                item.ToViewModel(s);
                viewModel.Students.Add(item);
            }

            return PartialView(viewModel);
        }

        #endregion

        #region Consultant Of Part

        [HttpGet]
        public ActionResult ConsultantOfPart()
        {
            var viewModel = new ConsultantOfPartListViewModel();
            var source = DataManager.ConsultantOfPartRepository.GetAll();
            viewModel.ToViewModel(source);
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult ConsultantOfPart(ConsultantOfPartListViewModel model)
        {
            if (model != null && model.ConsultantOfParts != null)
            {
                foreach (var item in model.ConsultantOfParts)
                {
                    DataManager.ConsultantOfPartRepository.Update(item.Id, item.Text);
                }
            }
            return RedirectToRoute("Default", new { controller = "SystemManagers", action = "ConsultantOfPart" });
        }

        public ActionResult AddConsultantOfPart(ConsultantOfPartViewModel model)
        {
            DataManager.ConsultantOfPartRepository.Create(model.Text);
            return RedirectToRoute("Default", new { controller = "SystemManagers", action = "ConsultantOfPart" });
        }

        public ActionResult DeleteConsultantOfPart(ConsultantOfPartViewModel model)
        {
            DataManager.ConsultantOfPartRepository.Delete(model.Id);
            return RedirectToRoute("Default", new { controller = "SystemManagers", action = "ConsultantOfPart" });
        }

        #endregion
    }
}